﻿using System.Collections.Generic;
using System.Linq;
using Microsoft.SharePoint;

namespace SPRepository
{
    public static class DtoUtilities
    {
        public static UserDto ConvertToDto(this SPUser user)
        {
            return new UserDto(user.Email, user.ID, user.IsApplicationPrincipal, user.IsDomainGroup, user.IsSiteAdmin,
                               user.IsSiteAuditor, user.LoginName, user.Name, user.Notes, user.RawSid,
                               user.RequireRequestToken, user.Sid);
        }

        public static List<TD> ConvertToDto<TT, TD>(IEnumerable<SPListItem> items)
            where TT : IContentType<TD>, new()
            where TD : class, IContentTypeDto
        {
            return items.Select(item => new TT().Get(item)).ToList();
        }

        public static List<TD> ConvertToDto<TT, TD>(SPListItemCollection items)
            where TT : IContentType<TD>, new()
            where TD : class, IContentTypeDto
        {
            return ConvertToDto<TT, TD>(items.Cast<SPListItem>());
        }
    }
}